package cn.yoyo.controller.adm;

import cn.yoyo.entity.Hot;
import cn.yoyo.tools.RespJson;
import cn.yoyo.service.HotService;
import cn.yoyo.service.LargeFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
public class HotController {
    @Autowired
    HotService hotService;
    @Autowired
    LargeFileService largeFileService;

    @RequestMapping("/api/adm/hot/findAll")
    public Map<String, Object> findAll() {
        return RespJson.pack(200, "", hotService.findAll());
    }

    @RequestMapping("/api/adm/hot/addHot")
    public Map<String, Object> addHot(Hot hot, @RequestParam("picx") MultipartFile file) {
        if (file == null || file.getSize() <= 0) {
            return RespJson.pack(400, "文件为空,添加失败", null);
        }
        if (hotService.addHot(hot, file) > 0) {
            return RespJson.pack(200, "添加成功", hotService.findAll());
        }
        return null;
    }

    @RequestMapping("/api/adm/hot/delete/{id}")
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        if (hotService.delete(id) > 0) {
            return RespJson.pack(200, "删除成功", hotService.findAll());
        }
        return null;
    }

}
